<template>
  <el-dialog title="提示" :visible.sync="showModify" width="30%" center>
    <el-form ref="form" :model="formData" label-width="80px" :rules="validateRules">
      <el-form-item label="楼宇名称" prop="buildingName">
        <el-input
          v-model="formData.buildingName"
          placeholder="请输入楼宇名称"
          maxlength="50"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="管家名称">
        <el-input
          v-model="formData.buildingManager"
          :rows="2"
          placeholder="请输入管家名称"
          maxlength="100"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="联系方式">
        <el-input
          v-model="formData.buildingManagerTel"
          :rows="2"
          placeholder="请输入管家联系方式"
          maxlength="100"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label="总楼层">
        <el-input-number v-model="formData.buildingFloor" controls-position="right" :min="1" :max="33"></el-input-number>
      </el-form-item>
      <el-form-item label="小区名称">
        <el-cascader v-model="formData.areaName" :options="areaList" ></el-cascader>
      </el-form-item>
      <el-form-item label="小区ID">
        <el-input
          v-model="formData.areaID"
          :rows="2"
          placeholder="请输入小区ID"
          maxlength="100"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onModify">确定</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      formData: {},
      showModify: false,
       areaList:[
        {value:"1",label:"印象城"},
        {value:"2",label:"佳馨花园"},
      ],
      validateRules: {
        buildingName: [
          { required: true, message: "请输入楼宇名称", trigger: "blur" }
        ],
        buildingManager: [
          { required: true, message: "请输入管家名称", trigger: "blur" }
        ],
        buildingManagerTel: [
          { required: true, message: "请输入管家联系方式", trigger: "blur" }
        ],
        buildingFloor: [{ required: true, message: "请输入共多少层", trigger: "blur" }],
        areaName: [
          { required: true, message: "请输入小区名称", trigger: "blur" }
        ],
        areaID: [{ required: true, message: "请输入小区ID", trigger: "blur" }]
      }
    };
  },
  methods: {
    ...mapActions("pms/base.building", ["addBuilding", "updateBuilding"]),
     showDialog(data = {}) {
      this.formData = data;
      this.showModify = true;
    },
   onModify() {
      console.log("onModify");
      console.log(this.formData);
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.formData.id) {
            this.updateBuilding(this.formData).then(() => {
              this.showModify = false;
              this.$emit("change");
              this.$message.success("修改成功");
            });
          } else {
            this.addBuilding(this.formData).then(() => {
              this.showModify = false;
              this.$emit("change");
              this.$message.success("添加成功");
            });
          }
        } else {
          console.log("验证失败");
        }
      });
    }
  }
};
</script>